package com.luo.common.restful;

import com.luo.common.exception.BBSServiceException;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    private Integer state;
    private String message;
    private T data;


    public static JsonResult<Void> ok() {
        return JsonResult.ok(null);
    }

    public static <T> JsonResult<T> ok(T data) {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setState(ResponseCode.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult<Void> fail(ResponseCode responseCode, String message) {
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.setState(responseCode.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }

    public static JsonResult<Void> fail(ResponseCode responseCode, Throwable e) {
        return JsonResult.fail(responseCode, e.getMessage());
    }


    public static JsonResult<Void> fail(BBSServiceException serviceException){
        return fail(serviceException.getResponseCode(),serviceException.getMessage());
    }
}
